package com.younion.action.manage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.younion.common.base.BaseAction;
import com.younion.common.paginator.Paginator;

import cpcns.jhsks.util.MapCollectionUtils;

import com.younion.business.IProductService;
import com.younion.vo.CustomerVO;
import com.younion.vo.ProductVO;

/**
 * @author wells
*/
public class ProductAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory.getLog(ProductAction.class);

	private IProductService productService = null;

	private ProductVO productVO = new ProductVO();

	private List<ProductVO> productList;// 结果集列表
	
	private String searchType;
	
	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	@RequiresPermissions("listProduct")
	public String listProduct() {
		try {
			Map paramMap = null;
			if(getSessionUser().getCustomer() != null){
				productVO.setCustomerVO(getSessionUser().getCustomer());
			}else if(productVO!=null && productVO.getCustomerId() != null){
				productVO.setCustomerVO(new CustomerVO(productVO.getCustomerId()));
			}
			if (productVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(productVO));
			}
			// 获取总记录数
			int total = productService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			productList = productService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
			this.addLog(2, "查询商品列表", "查询商品列表", this.getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	@RequiresPermissions("addProduct")
	public String addProduct() {
		return SUCCESS;
	}

	/**
	 * 修改保存，调度Jsp
	 */
	@RequiresPermissions("editProduct")
	public String editProduct() {
		try {
			productVO = productService.getById(productVO.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存动作
	 */
	@RequiresPermissions("saveProduct")
	public String saveProduct() {
		try {
			productVO = productService.save(productVO);
			String id = productVO.getId() == null ? "" : productVO.getId().toString();
			String log = "".equals(id) ? "添加商品列表" : "更新商品列表";
			int type = "".equals(id) ? 3:4;
			this.addLog(type, log+id, log,getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "productList");
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("deleteProduct")
	public String deleteProduct() {
		try {
			String pkId = request.getParameter("ids");
			productService.remove(Arrays.asList(pkId.split(",")));
			String log = "删除商品列表";
			this.addLog(5, log+pkId, log,getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "productList");
		return SUCCESS;
	}
	/**
	 * 查看鿴
	 */
	@RequiresPermissions("viewProduct")
	public String viewProduct() {
		try {
			String pkId = request.getParameter("ids"); 
			productVO = productService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public ProductVO getProductVO() {
		return productVO;
	}

	public void setProductVO(ProductVO productVO) {
		this.productVO = productVO;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	public List<ProductVO> getProductList() {
		return productList;
	}

	public void setProductList(List<ProductVO> productList) {
		this.productList = productList;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

}